# CMakeLists.txt


# This is the LGPL libmariadb project.
PROJECT(mariadb-connector-c C)

SET(PACKAGE_STATUS_SUFFIX "alpha")

SET(CPACK_PACKAGE_VERSION_MAJOR 3)
SET(CPACK_PACKAGE_VERSION_MINOR 1)
SET(CPACK_PACKAGE_VERSION_PATCH 0)
SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)

IF(COMMAND CMAKE_POLICY)
  SET(OLD_POLICIES CMP0026 CMP0042 CMP0045)
  SET(NEW_POLICIES CMP0003 CMP0022 CMP0023)
  FOREACH(TYPE OLD NEW)
    FOREACH(P ${${TYPE}_POLICIES})
      IF(POLICY ${P})
        CMAKE_POLICY(SET ${P} ${TYPE})
      ENDIF()
    ENDFOREACH()   
  ENDFOREACH()
ENDIF()


SET(MARIADB_CONNECTOR_C_COPYRIGHT "2013-2016 MariaDB Corporation Ab")

### Options ###
IF(NOT WIN32)
  OPTION(WITH_MYSQLCOMPAT "creates libmysql* symbolic links" ON)
ELSE()
  OPTION(WITH_MSI "Build MSI installation package" OFF)
  OPTION(WITH_SIGNCODE "digitally sign files" OFF)
  OPTION(WITH_RTC "enables run time checks for debug builds" OFF)
ENDIF()

OPTION(WITH_UNITTEST "build test suite" ON)
OPTION(WITH_DYNCOL "Enables support of dynamic coluumns" ON)
OPTION(WITH_EXTERNAL_ZLIB "Enables use of external zlib" OFF)
###############
IF(WITH_SIGNCODE)
  IF(WIN32)
    SET(SIGN_OPTIONS /a /t http://timestamp.verisign.com/scripts/timstamp.dll)
  ENDIF()
  MARK_AS_ADVANCED(SIGN_OPTIONS)
ENDIF()

IF(WITH_RTC)
  SET(RTC_OPTIONS "/RTC1 /RTCc")
ENDIF()

INCLUDE(FindCURL)
IF(CURL_FOUND)
  ADD_DEFINITIONS(-DHAVE_CURL=1)
ENDIF()

IF(NOT WIN32)
  INCLUDE(${PROJECT_SOURCE_DIR}/cmake/FindGSSAPI.cmake)
  IF(GSSAPI_FOUND)
    INCLUDE_DIRECTORIES(${GSSAPI_INCS})
  ENDIF()
ENDIF()

INCLUDE(${PROJECT_SOURCE_DIR}/cmake/plugins.cmake)

IF(AUTH_GSSAPI_PLUGIN_TYPE MATCHES "STATIC")
  SET(SYSTEM_LIBS ${SYSTEM_LIBS} ${GSSAPI_LIBS})
ENDIF()


IF(WIN32)
  FILE(REMOVE ${PROJECT_BINARY_DIR}/win/packaging/plugin.conf)
  INCLUDE(${PROJECT_SOURCE_DIR}/cmake/version_info.cmake)
ENDIF()

IF(MSVC)
  # Speedup system tests
  INCLUDE(${PROJECT_SOURCE_DIR}/cmake/WindowsCache.cmake)
  IF (MSVC)
    SET(CONFIG_TYPES "DEBUG" "RELEASE" "RELWITHDEBINFO")
    FOREACH(BUILD_TYPE ${CONFIG_TYPES})
      FOREACH(COMPILER CXX C)
        SET(COMPILER_FLAGS "${CMAKE_${COMPILER}_FLAGS_${BUILD_TYPE}}")
        IF (NOT COMPILER_FLAGS STREQUAL "")
          STRING(REPLACE "/MD" "/MT" COMPILER_FLAGS ${COMPILER_FLAGS})
          IF (CMAKE_BUILD_TYPE STREQUAL "Debug")
            SET(COMPILER_FLAGS "${COMPILER_FLAGS} ${RTC_OPTIONS}")
            STRING(REPLACE "/Zi" "/ZI" COMPILER_FLAGS ${COMPILER_FLAGS})
          ENDIF()
          MESSAGE (STATUS "CMAKE_${COMPILER}_FLAGS_${BUILD_TYPE}= ${COMPILER_FLAGS}") 
          SET(CMAKE_${COMPILER}_FLAGS_${BUILD_TYPE} ${COMPILER_FLAGS} CACHE 
               STRING "overwritten by libmariadb" FORCE)
        ENDIF()
      ENDFOREACH()
    ENDFOREACH()
  ENDIF()
ENDIF()

# Disable dbug information for release builds 
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DDBUG_OFF")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DDBUG_OFF")
SET(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -DDBUG_OFF")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -DDBUG_OFF")

IF(CMAKE_COMPILER_IS_GNUCC)
  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunused -Wno-uninitialized")
ENDIF()

# If the build type isn't specified, set to Relwithdebinfo as default.
IF(NOT CMAKE_BUILD_TYPE)
  SET(CMAKE_BUILD_TYPE "RelWithDebInfo")
ENDIF()

# various defines for generating include/mysql_version.h

SET(PROTOCOL_VERSION 10) # we adapted new password option from PHP's mysqlnd !
SET(MARIADB_CLIENT_VERSION_MAJOR "10")
SET(MARIADB_CLIENT_VERSION_MINOR "1")
SET(MARIADB_CLIENT_VERSION_PATCH "7")
SET(MARIADB_CLIENT_VERSION "${MARIADB_CLIENT_VERSION_MAJOR}.${MARIADB_CLIENT_VERSION_MINOR}.${MARIADB_CLIENT_VERSION_PATCH}")
MATH(EXPR MARIADB_VERSION_ID "${MARIADB_CLIENT_VERSION_MAJOR} * 10000 +
                              ${MARIADB_CLIENT_VERSION_MINOR} * 100   +
                              ${MARIADB_CLIENT_VERSION_PATCH}")
IF (NOT MARIADB_PORT)
  SET(MARIADB_PORT 3306)
ENDIF ()
IF(NOT MARIADB_UNIX_ADDR)
  SET(MARIADB_UNIX_ADDR "/tmp/mysql.sock")
ENDIF()

INCLUDE("${PROJECT_SOURCE_DIR}/cmake/install.cmake")

# todo: we don't character sets in share - all is compiled in
SET(SHAREDIR "share")
SET(DEFAULT_CHARSET_HOME "${CMAKE_INSTALL_PREFIX}")
# SET(MARIADB_PLUGINDIR "${CMAKE_INSTALL_PREFIX}/lib/plugin")

INCLUDE(${PROJECT_SOURCE_DIR}/cmake/SearchLibrary.cmake)

IF(WITH_EXTERNAL_ZLIB)
  FIND_PACKAGE(ZLIB QUIET)
  IF(${ZLIB_FOUND})
    SET(LIBZ "-z")
  ENDIF()
ENDIF()


IF(UNIX)
  SEARCH_LIBRARY(LIBDL dlopen "dl")
  SEARCH_LIBRARY(LIBM floor m)
  SEARCH_LIBRARY(LIBPTHREAD pthread_getspecific "pthread;pthreads")
  SEARCH_LIBRARY(LIBNSL gethostbyname_r "nsl_r;nsl")
  SEARCH_LIBRARY(LIBSOCKET setsockopt socket)
  FIND_PACKAGE(Threads)
  SET(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${LIBNSL} ${LIBBIND} ${LIBICONV}
    ${LIBSOCKET} ${LIBDL} ${LIBM} ${LIBPTHREAD})
  SET(SYSTEM_LIBS ${LIBNSL} ${LIBBIND} ${LIBICONV}
    ${LIBSOCKET} ${LIBDL} ${LIBM} ${LIBPTHREAD})
  MESSAGE(STATUS "SYSTEM_LIBS ${SYSTEM_LIBS}")
  #remove possible dups from required libraries
  LIST(LENGTH CMAKE_REQUIRED_LIBRARIES rllength)
  IF(${rllength} GREATER 0)
    LIST(REMOVE_DUPLICATES CMAKE_REQUIRED_LIBRARIES)
  ENDIF()
ENDIF()


IF(CMAKE_HAVE_PTHREAD_H)
  SET(CMAKE_REQUIRED_INCLUDES pthread.h)
ENDIF()

IF(DBUG_OFF)
  ADD_DEFINITIONS(-DDBUG_OFF=1)
ENDIF()

IF(WIN32)
  SET(HAVE_THREADS 1)
  ADD_DEFINITIONS(-DHAVE_DLOPEN)
  ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
  IF(MSVC)
     SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996" )
  ENDIF()
ELSEIF()
  SET(HAVE_THREADS ${CMAKE_USE_PTHREADS})
ENDIF()

# check for various include files
INCLUDE(${PROJECT_SOURCE_DIR}/cmake/CheckIncludeFiles.cmake)
# check for various functions
INCLUDE(${PROJECT_SOURCE_DIR}/cmake/CheckFunctions.cmake)
# check for various types
INCLUDE(${PROJECT_SOURCE_DIR}/cmake/CheckTypes.cmake)

IF(NOT WITH_SSL AND NOT WITH_SSL STREQUAL "OFF")
  IF(WIN32)
    SET(WITH_SSL "SCHANNEL")
  ELSE()
    SET(WITH_SSL "OPENSSL")
  ENDIF()
ENDIF()

IF(NOT WITH_SSL STREQUAL "OFF")
  IF(WITH_SSL STREQUAL "OPENSSL")
    FIND_PACKAGE(OpenSSL)
    IF(OPENSSL_FOUND)
      ADD_DEFINITIONS(-DHAVE_OPENSSL -DHAVE_TLS)
      SET(SSL_SOURCES "${PROJECT_SOURCE_DIR}/libmariadb/secure/openssl.c")
      SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES} ${OPENSSL_CRYPTO_LIBRARIES})
      INCLUDE_DIRECTORIES(BEFORE ${OPENSSL_INCLUDE_DIR})
      MESSAGE(STATUS "OpenSSL version: ${OPENSSL_VERSION}")
    ELSE()
      MESSAGE(WARNING "OpenSSL not found")
    ENDIF()
  ENDIF()
  IF(WITH_SSL STREQUAL "GNUTLS")
    FIND_PACKAGE(GnuTLS)
    IF(GNUTLS_FOUND)
      ADD_DEFINITIONS(-DHAVE_GNUTLS -DHAVE_TLS)
      SET(SSL_SOURCES "${PROJECT_SOURCE_DIR}/libmariadb/secure/gnutls.c")
      SET(SSL_LIBRARIES ${GNUTLS_LIBRARY})
    ELSE()
      MESSAGE(FATAL "GnuTLS not found")
    ENDIF()
  ENDIF()
  IF(WIN32)
    IF(WITH_SSL STREQUAL "SCHANNEL")
      ADD_DEFINITIONS(-DHAVE_SCHANNEL -DHAVE_TLS)
      SET(SSL_SOURCES "${PROJECT_SOURCE_DIR}/libmariadb/secure/schannel.c" "${PROJECT_SOURCE_DIR}/libmariadb/secure/ma_schannel.c")
      INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/plugins/pvio/")
    ENDIF()
  ENDIF()

  MARK_AS_ADVANCED(SSL_SOURCES)
ENDIF()

IF(NOT WIN32)
  INCLUDE(${PROJECT_SOURCE_DIR}/cmake/FindIconv.cmake)
ENDIF()

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/include/ma_config.h.in
               ${CMAKE_CURRENT_BINARY_DIR}/include/ma_config.h)
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/include/mariadb_version.h.in
               ${CMAKE_CURRENT_SOURCE_DIR}/include/mariadb_version.h)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/include)

IF(WIN32)
  SET(SYSTEM_LIBS ws2_32 advapi32 kernel32)
ELSE()
  SET(SYSTEM_LIBS ${LIBPTHREAD} ${LIBDL} ${LIBM})
  IF(ICONV_EXTERNAL)
    SET(SYSTEM_LIBS ${SYSTEM_LIBS} ${ICONV_LIBRARIES})
  ENDIF()
ENDIF()
IF(WITH_SSL)
  SET(SYSTEM_LIBS ${SYSTEM_LIBS} ${SSL_LIBRARIES})
ENDIF()
MARK_AS_ADVANCED(SYSTEM_LIBS)

IF(NOT REMOTEIO_PLUGIN_TYPE MATCHES "OFF")
  IF(CURL_FOUND)
    INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS})
    IF(REMOTEIO_PLUGIN_TYPE MATCHES "STATIC")
      SET(SYSTEM_LIBS ${SYSTEM_LIBS} ${CURL_LIBRARIES})
    ENDIF()
    ADD_DEFINITIONS("-DHAVE_REMOTEIO=1")
  ENDIF()
ENDIF()
IF(NOT WIN32)
  IF(NOT AUTH_GSSAPI_PLUGIN_TYPE MATCHES "OFF")
    INCLUDE(${PROJECT_SOURCE_DIR}/cmake/FindGSSAPI.cmake)
    IF(GSSAPI_FOUND)
      INCLUDE_DIRECTORIES(${GSSAPI_INCS})
      IF(AUTH_GSSAPI_PLUGIN_TYPE MATCHES "STATIC")
        SET(SYSTEM_LIBS ${SYSTEM_LIBS} ${GSSAPI_LIBS})
      ENDIF()
    ENDIF()
  ENDIF()
ENDIF()
ADD_SUBDIRECTORY(include)
ADD_SUBDIRECTORY(libmariadb)
ADD_SUBDIRECTORY(plugins)
IF(NOT WIN32)
  ADD_SUBDIRECTORY(mariadb_config)
ENDIF()

IF(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/unittest)
  IF(WITH_UNITTEST STREQUAL "ON")
    ADD_SUBDIRECTORY(unittest/mytap)
    ADD_SUBDIRECTORY(unittest/libmariadb)
  ENDIF()
ENDIF()

IF(CLIENT_DOCS)
  INSTALL(DIRECTORY ${CLIENT_DOCS}
          DESTINATION ${DOCS_INSTALL_DIR_${INSTALL_LAYOUT}})
ENDIF()


IF(WIN32 AND WITH_MSI AND CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
  DD_SUBDIRECTORY(win/packaging)
ENDIF()

SET(CPACK_PACKAGE_VENDOR "MariaDB Corporation Ab")
SET(CPACK_PACKAGE_DESCRIPTION "MariaDB Connector/C. A library for connecting to MariaDB and MySQL servers")
SET(CPACK_PACKAGE_NAME "mariadb_connector_c")
STRING(TOLOWER ${CMAKE_SYSTEM_NAME} system_name)
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/COPYING.LIB")
SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README")
INCLUDE(cmake/ConnectorName.cmake)
IF(NOT PACKAGE_STATUS_SUFFIX)
  SET(CPACK_SOURCE_PACKAGE_FILE_NAME "mariadb-connector-c-${CPACK_PACKAGE_VERSION}-src")
  SET(CPACK_PACKAGE_FILE_NAME "mariadb-connector-c-${CPACK_PACKAGE_VERSION}-${system_name}-${CMAKE_SYSTEM_PROCESSOR}")
ELSE()
  SET(CPACK_SOURCE_PACKAGE_FILE_NAME "mariadb-connector-c-${CPACK_PACKAGE_VERSION}-${PACKAGE_STATUS_SUFFIX}-src")
  SET(CPACK_PACKAGE_FILE_NAME "mariadb-connector-c-${CPACK_PACKAGE_VERSION}-${PACKAGE_STATUS_SUFFIX}-${system_name}-${CMAKE_SYSTEM_PROCESSOR}")
ENDIF()
# Build source packages
IF(GIT_BUILD_SRCPKG)
  # get branch name
  EXECUTE_PROCESS(COMMAND git show-branch OUTPUT_VARIABLE git_branch)
  STRING(REGEX MATCH "\\[([^]]+)\\]" git_branch ${git_branch})
  STRING(REGEX REPLACE "\\[|\\]" "" GIT_BRANCH ${git_branch})
  MESSAGE(STATUS "${GIT_BRANCH}")
  IF(WIN32)
    EXECUTE_PROCESS(COMMAND git archive ${GIT_BRANCH} --format=zip --prefix=${CPACK_SOURCE_PACKAGE_FILE_NAME}/ --output=${CPACK_SOURCE_PACKAGE_FILE_NAME}.zip)
  ELSE()
    EXECUTE_PROCESS(COMMAND git archive ${GIT_BRANCH} --format=zip --prefix=${CPACK_SOURCE_PACKAGE_FILE_NAME}/ --output=${CPACK_SOURCE_PACKAGE_FILE_NAME}.zip)
    EXECUTE_PROCESS(COMMAND git archive ${GIT_BRANCH} --format=tar --prefix=${CPACK_SOURCE_PACKAGE_FILE_NAME}/ --output=${CPACK_SOURCE_PACKAGE_FILE_NAME}.tar)
    EXECUTE_PROCESS(COMMAND gzip -9 -f ${CPACK_SOURCE_PACKAGE_FILE_NAME}.tar)
  ENDIF()
ENDIF()

SET(CPACK_SOURCE_IGNORE_FILES
\\\\.git/
\\\\.gitignore
\\\\.gitattributes
CMakeCache\\\\.txt
cmake_dist\\\\.cmake
CPackConfig\\\\.cmake
mariadb_config\\\\.c$
\\\\.build/
html/
unittest
/cmake_install.cmake
/CTestTestfile.cmake
/CPackSourceConfig.cmake
/CMakeFiles/
/version_resources/
/_CPack_Packages/
\\\\.gz$
\\\\.zip$
mariadb_config/mariadb_config$
/CMakeFiles/
/version_resources/
/_CPack_Packages/
Makefile$
include/my_config\\\\.h$
)

IF(WITH_TEST_SRCPKG)
  SET(PACKAGE_FILE ${PROJECT_SOURCE_DIR}/package.name)
  FILE(REMOVE ${PACKAGE_FILE})
  FILE(WRITE ${PACKAGE_FILE} ${CPACK_SOURCE_PACKAGE_FILE_NAME})
ENDIF()

IF(WIN32)
  SET(CPACK_GENERATOR "ZIP")
  SET(CPACK_SOURCE_GENERATOR "ZIP")
ELSE()
  SET(CPACK_GENERATOR "TGZ")
  SET(CPACK_SOURCE_GENERATOR "TGZ")
ENDIF()
INCLUDE(CPack)

MESSAGE(STATUS "MariaDB Connector/c configuration:")
MESSAGE(STATUS "CPack generation: ${CPACK_GENERATOR}")
IF(CLIENT_DOCS)
  MESSAGE(STATUS "Documentation included from ${CLIENT_DOCS}")
ENDIF()
MESSAGE(STATUS "SSL support: ${WITH_SSL} Libs: ${SSL_LIBRARIES}")
MESSAGE(STATUS "Experimental Sqlite support: ${WITH_SQLITE}")
IF(WITH_EXTERNAL_ZLIB)
  MESSAGE(STATUS "Zlib support: ${WITH_EXTERNAL_ZLIB}")
ELSE()
  MESSAGE(STATUS "Zlib support: yes (using bundled zlib)")
ENDIF()
MESSAGE(STATUS "Installation layout: ${INSTALL_LAYOUT}")
MESSAGE(STATUS "Include files will be installed in ${PREFIX_INSTALL_DIR}/${INCLUDE_INSTALL_DIR}/${SUFFIX_INSTALL_DIR}")
MESSAGE(STATUS "Libraries will be installed in ${PREFIX_INSTALL_DIR}/${LIB_INSTALL_DIR}/${SUFFIX_INSTALL_DIR}")
MESSAGE(STATUS "Binaries will be installed in ${PREFIX_INSTALL_DIR}/${BIN_INSTALL_DIR}")

MESSAGE(STATUS "Required: ${CMAKE_REQUIRED_LIBRARIES}")
